package regex;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {
	public static void main(String[] args) {
		String regex = "(?m)(\\w+)\\W+((\\w+)\\W+(\\w+))\\W?$"; //(?m)是个模式标记，告诉系统注意每行的\n也算作$
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher("Twas brillig, and the slithy woves\n" +
				        "Did gyre and gimble in the wabe.\n" +
						"All mimsy were the borogoves,\n" +
						"And the mome raths outgrabe.\n\n" +
						"Beware the Jabberwock, my son,\n" +
						"The jaws that bite, the cloaws that catch.\n" +
						"Beware the Jubjub bird, and shun\n" +
						"The frumious Bandersnatch.");
		
		while(matcher.find()) {
			System.out.println("group: " + matcher.group());
			System.out.println("start: " + matcher.start());
			System.out.println("end: " + matcher.end());
			System.out.println();
			for(int i=0; i<= matcher.groupCount(); i++) {
				System.out.println("group" + i +": " +  matcher.group(i));
				System.out.println("start: " + matcher.start(i));
				System.out.println("end: " + matcher.end(i));
				System.out.println();
			}
			System.out.println("hitEnd: " + (matcher.hitEnd() ? "true" : "false"));
			System.out.println("regionStart: " + matcher.regionStart());
			System.out.println("regionEnd: " + matcher.regionEnd());
			System.out.println();
		}

		int i = 0;
		while(matcher.find(i)) {
			System.out.println(matcher.group() + " ");
			i++;
		}
		
	}
}

class Rt1 {
	public static void main(String[] args) {
		String regex = ".*(hi).*";
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher("Yes This is a Test");
		
		while(matcher.find()) {
			System.out.println("group: " + matcher.group());
			System.out.println("start: " + matcher.start());
			System.out.println("end: " + matcher.end());
			System.out.println();
			for(int i=0; i< matcher.groupCount(); i++) {
				System.out.println("group" + i +": " +  matcher.group(i));
				System.out.println("start: " + matcher.start(i));
				System.out.println("end: " + matcher.end(i));
				System.out.println();
			}
			System.out.println("hitEnd: " + (matcher.hitEnd() ? "true" : "false"));
			System.out.println("regionStart: " + matcher.regionStart());
			System.out.println("regionEnd: " + matcher.regionEnd());
			System.out.println();
		}

		int i = 0;
		while(matcher.find(i)) {
			System.out.println(matcher.group() + " ");
			i++;
		}
	}
}

class Rt2 {
	
}

class RegSplitTest {
	public static void main(String[] args) {
		String sip = "127;0^0_1";
		System.out.println(Arrays.asList(sip.split("\\p{Punct}")));
		
		Pattern p = Pattern.compile("\\p{Punct}");
		System.out.println(Arrays.asList(p.split(sip)));
	}
}

class ReplaceTest {
	public static void main(String[] args) {
		String s = "Four years have pasted since the mortal races bounded together, understood the might of the burning legion.";
		
		String regex = "\\s(s)";
		
		Pattern p = Pattern.compile(regex);
		System.out.println(p.matcher(s).replaceAll(" S"));
	}
}

class ReplaceTestII {
	public static void main(String[] args) {
		String s = "Four years have pasted since the mortal races bounded together, understood against the might of the burning legion.";
		
		String regex = "\\s(s)";
		
		StringBuffer result = new StringBuffer();
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(s);
		while(m.find()) {
			m.appendReplacement(result, " 【$1】");
		}
		m.appendTail(result);
		System.out.println(result.toString());
	}
}

class jt {
	public static void main(String[] args) {
		List l = new ArrayList();
		l.add(new ReplaceTestII());
		Object o = l.get(0);
		ReplaceTestII real = (ReplaceTestII)o;
		
		System.out.println(real == o);
	}
}

class jt2 {
	public static void main(String[] args) {
		String s = "20120101";
		String regex = "(\\d{6})\\d{2}";
		System.out.println(Pattern.compile(regex).matcher(s).matches());
		Matcher m = Pattern.compile(regex).matcher(s);
		m.matches();
		System.out.println(m.group(1));
	}
}

class jt3 {
	public static void main(String[] args) {
		System.out.println("WhEre 1=1".replaceAll("(?i).*where", " and"));
	}
}

class jt4 {
	public static void main(String[] args) {
		String s = "林可";
		if(s.matches("林.*可")) {
			System.out.println("0.0");
		}
	}
}

class jt5 {
	public static void main(String[] args) {
		System.out.println(isValidTableName("_12313"));
		System.out.println(isValidTableName("12313"));
		System.out.println(isValidTableName("12313ABC"));
		System.out.println(isValidTableName("ABC12313"));
	}
	
	public static boolean isValidTableName(String tableName) {
		if( null == tableName)
			return false;
		if(tableName.getBytes().length > 30 ) {
			return false;
		}
		// String regex = "^[a-zA-Z\\u????-\\u????][\\w\\u????-\\u????]*$";
		String regex = "^[a-zA-Z]\\w*$";
		if(tableName.matches(regex)) {
			return true;
		} else
			return false;
	}
}

class jt6 {
	public static void main(String[] args) {
		System.out.println(Arrays.asList("One, Two Three, Four Five".split("[' '',']")));
		System.out.println(Arrays.asList("One, Two Three, Four Five".split("[ ,]")));
	}
}

class t21 {
	public static void main(String[] args) {
		System.out.println("科目< br />编号".replaceAll("< *br */{0,1}>", ""));
		System.out.println("借方< br/>金额".replaceAll("< *br */{0,1}>", ""));
		System.out.println("记账<br/>凭证".replaceAll("<[ ]*br[ ]*[/]{0,1}>", ""));
		System.out.println("辅助<br>明细".replaceAll("<[ ]*br[ ]*[/]{0,1}>", ""));
		System.out.println("辅助内容< br >编号".replaceAll("<[ ]*br[ ]*[/]{0,1}>", ""));
		System.out.println("< b r >".replaceAll("<[ ]*br[ ]*[/]{0,1}>", ""));
		
		System.out.println("sdfsf我我".toUpperCase());
	}
}

class t22 {
	public static void main(String[] args) {
		Pattern p = Pattern.compile("[\\d]*\\.[\\d]*");
		System.out.println(p.matcher("\\").matches());
		System.out.println(p.matcher("1").matches());
		System.out.println(p.matcher("d").matches());
		System.out.println(p.matcher("1.000").matches());
	}
}

class t23 {
	public static void main(String[] args) {
		Pattern p = Pattern.compile("[\\p{Punct}]");
		System.out.println(p.matcher("*").matches());
		System.out.println(p.matcher("a").matches());
	}
}

class t24 {
	public static void main(String[] args) {
		// System.out.println(modifyFormula("-1234.56*-9+823-(-992*98)/-2*3"));
		System.out.println(modifyFormula("(-2-4)/2"));
		System.out.println(modifyFormula("-(-2-4)/2"));
	}
	
	public static String modifyFormula(String formula) {
		Pattern p = Pattern.compile("[\\d.]+|[()+\\-*/]");
		List<String> tempFactorList = new LinkedList<String>();
		Matcher m = p.matcher(formula);
		while(m.find()) {
			tempFactorList.add(m.group());
		}
		System.out.println(tempFactorList);
		List<String> factorList = new LinkedList<String>();
		boolean getNegative = true;
		for(int i=0; i<tempFactorList.size(); i++) {
			String factor = tempFactorList.get(i);
			if(getNegative) {
				if("-".equals(factor)) {
					factorList.add("0");
				}
				getNegative = false;
			}
			if("(".equals(factor)) {
				getNegative = true;
			}
			factorList.add(factor);
		}
		System.out.println(factorList);
		StringBuilder sbuilder = new StringBuilder();
		for(String str : factorList) {
			sbuilder.append(str);
		}
		String tempResult = sbuilder.toString();
		System.out.println("temp:" + tempResult);
		tempResult = tempResult.replaceAll("([*/])-([\\d.]*)", "$1(0-$2)");
		return tempResult;
	}
}

class t25 {
	public static void main(String[] args) {
		System.out.println(modifyFormula("(-2-4)/2"));
	}
	
	public static String modifyFormula(String formula) {
		Pattern p = Pattern.compile("[\\d.]+|[()+\\-*/]");
		List<String> tempFactorList = new LinkedList<String>();
		Matcher m = p.matcher(formula);
		while(m.find()) {
			tempFactorList.add(m.group());
		}
		System.out.println(tempFactorList);
		List<String> factorList = new LinkedList<String>();
		boolean getNegative = true;
		for(int i=0; i<tempFactorList.size(); i++) {
			String factor = tempFactorList.get(i);
			if(getNegative) {
				if("-".equals(factor)) {
					factorList.add("0");
				}
				getNegative = false;
			}
			if("(".equals(factor)) {
				getNegative = true;
			}
			factorList.add(factor);
		}
		System.out.println(factorList);
		StringBuilder sbuilder = new StringBuilder();
		for(String str : factorList) {
			sbuilder.append(str);
		}
		String tempResult = sbuilder.toString();
		System.out.println("temp:" + tempResult);
		tempResult = tempResult.replaceAll("([*/])-([\\d.]*)", "$1(0-$2)");
		return tempResult;
	}
}

class groupTest {
	public static void main(String[] args) {
		Pattern p = Pattern.compile("-?\\d*.{0,1}\\d*");
		Matcher m = p.matcher("100.000113123");
		System.out.println(m.matches());
	}
}

class t88 {
	public static void main(String[] args) {
		Pattern p = Pattern.compile("[0-9.]");
		System.out.println(p.matcher("0").matches());
		System.out.println(p.matcher("2").matches());
		System.out.println(p.matcher("9").matches());
		System.out.println(p.matcher(".").matches());
		System.out.println(p.matcher("10").matches());
	}
}

class temp111 {
	public static void main(String[] args) {
		System.out.println(ttt("&nbsp;&nbsp;中&lt;文&amp;"));
	}
	
	private static String ttt(String s) {
		return s.replaceAll("&\\w+;", "");
	}
}